package com.ruoyi.supermarket.service.impl;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.supermarket.domain.*;
import com.ruoyi.supermarket.domain.Vo.ShCPay;
import com.ruoyi.supermarket.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.ParameterResolutionDelegate;
import org.springframework.stereotype.Service;
import com.ruoyi.supermarket.service.IYfpayService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 应付汇总Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-05-30
 */
@Service
public class YfpayServiceImpl implements IYfpayService 
{
    @Autowired
    private YfpayMapper yfpayMapper;

    @Autowired
    private XqyfpayMapper xqyfpayMapper;

    @Autowired
    private PurchaseMapper purchaseMapper;
    @Autowired
    private XqpurchaseMapper xqpurchaseMapper;
    @Autowired
    private QkMapper qkMapper;

    /**
     * 查询应付汇总
     * 
     * @param yfpayId 应付汇总主键
     * @return 应付汇总
     */
    @Override
    public Yfpay selectYfpayByYfpayId(Long yfpayId)
    {
        return yfpayMapper.selectYfpayByYfpayId(yfpayId);
    }

    /**
     * 查询应付汇总列表
     * 
     * @param yfpay 应付汇总
     * @return 应付汇总
     */
    @Override
    public List<Yfpay> selectYfpayList(Yfpay yfpay)
    {
        return yfpayMapper.selectYfpayList(yfpay);
    }

    /**
     * 新增应付汇总
     * 
     * @param yfpay 应付汇总
     * @return 结果
     */
    @Override
    public int insertYfpay(Yfpay yfpay)
    {
        yfpay.setCreateTime(LocalDateTime.now());
        return yfpayMapper.insertYfpay(yfpay);
    }

    /**
     * 修改应付汇总
     * 
     * @param yfpay 应付汇总
     * @return 结果
     */
    @Override
    public int updateYfpay(Yfpay yfpay)
    {
        yfpay.setIsSh("1");
        return yfpayMapper.updateYfpay(yfpay);
    }

    /**
     * 批量删除应付汇总
     * 
     * @param yfpayIds 需要删除的应付汇总主键
     * @return 结果
     */
    @Override
    public int deleteYfpayByYfpayIds(Long[] yfpayIds)
    {
        return yfpayMapper.deleteYfpayByYfpayIds(yfpayIds);
    }

    /**
     * 删除应付汇总信息
     * 
     * @param yfpayId 应付汇总主键
     * @return 结果
     */
    @Override
    public int deleteYfpayByYfpayId(Long yfpayId)
    {
        return yfpayMapper.deleteYfpayByYfpayId(yfpayId);
    }

    @Override
    public AjaxResult audit(ShCPay shCPay) {
        Yfpay yfpay = yfpayMapper.selectYfpayByYfpayId(shCPay.getYfpayId());
        yfpay.setIsSh(shCPay.getIsSh());
        yfpay.setShName(SecurityUtils.getLoginUser().getUsername());
        yfpay.setShTime(LocalDateTime.now());
        yfpayMapper.updateYfpay(yfpay);
        return AjaxResult.success();
    }

    @Override
    @Transactional
    public AjaxResult hxPay(Yfpay yfpay) {
        Yfpay y = new Yfpay();
        y.setIsHx("1");
        y.setYfpayId(yfpay.getYfpayId());
        y.setInvoice(yfpay.getInvoice());
        y.setPayTime(LocalDateTime.now());
        yfpayMapper.updateYfpay(y);
       Yfpay yf =  yfpayMapper.selectYfpayByYfpayId(yfpay.getYfpayId());
        Qk qk = qkMapper.selectBySupplyId(yf.getSupplyId());
        qk.setQkPrice(qk.getQkPrice().subtract(yf.getZprice()));
        return AjaxResult.success();
    }

    @Override
    @Transactional
    public AjaxResult Fzd(Yfpay yfpay) {
        Xqyfpay xqyfpay = new Xqyfpay();
        xqyfpay.setYspayId(yfpay.getYfpayId());
        List<Xqyfpay> list = xqyfpayMapper.selectXqyfpayList(xqyfpay);
        for (Xqyfpay x : list) {
            Xqpurchase xqpurchase = xqpurchaseMapper.selectXqpurchaseByPId(x.getpId());
            Purchase purchase = purchaseMapper.selectPurchaseByPurchaseId(xqpurchase.getPurchaseId());
            purchase.setIsZd("0");
            purchaseMapper.updatePurchase(purchase);
        }
        yfpayMapper.deleteYfpayByYfpayId(yfpay.getYfpayId());
        xqyfpayMapper.deleteXqyfpayByYspayId(yfpay.getYfpayId());
        return AjaxResult.success();
    }

    @Override
    public List<Yfpay> selectYfpayListFzd(Yfpay yfpay) {
        List<String> listSh = new ArrayList<String>();
        listSh.add("1");
        listSh.add("2");
        yfpay.setIsShList(listSh);
        return yfpayMapper.selectYfpayListFzd(yfpay);
    }
}
